package com.nbjtjc.safe.api.emg.goods;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgGoodsTypeMapper;
import com.nbjtjc.safe.model.emg.EmgGoodsType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 物资类型
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/emgGoodsType")
public class GoodsTypeController extends AbstractController<EmgGoodsType> {

    @Autowired
    private EmgGoodsTypeMapper emgGoodsTypeMapper;

    @Override
    @Transactional(rollbackFor = {Throwable.class})
    @RequestMapping(value = "/delete",method = {RequestMethod.POST})
    @SysLog("删除物资类型")
    public Result delete(int id) {
        Result result=new Result();
        try {
            emgGoodsTypeMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Transactional(rollbackFor = {Throwable.class})
    @RequestMapping(value = "/addOrUpdate",method = {RequestMethod.POST})
    @SysLog("添加物资类型")
    public Result add(EmgGoodsType emgGoodsType,Page page) {
        Result result=new Result();
        if(emgGoodsType==null){
            return result;
        }
        try {
            if(emgGoodsType.getId()!=null){
                emgGoodsTypeMapper.updateByPrimaryKeySelective(emgGoodsType);
                result.setData(emgGoodsType);
                return result;
            }
            List<EmgGoodsType> emgGoodsTypes=emgGoodsTypeMapper.search(emgGoodsType,page);
            if(emgGoodsTypes!=null&&emgGoodsTypes.size()>0){
                result.setFalse("该类型已经存在");
                return result;
            }
            emgGoodsTypeMapper.insert(emgGoodsType);
            result.setData(emgGoodsType);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @Override
    @RequestMapping(value = "/find",method = {RequestMethod.POST})
    public Result find(EmgGoodsType emgGoodsType, Page page) {
        Result result=new Result();
        PageHelper.startPage(page.getPageCurrent(),page.getPageSize());
        List<EmgGoodsType> emgGoodsTypes= null;
        try {
            emgGoodsTypes = emgGoodsTypeMapper.search(emgGoodsType,page);
            result.setData(new PageInfo<>(emgGoodsTypes));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
}
